package windowFactoryPackage;

import java.awt.Color;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.ProgressMonitor;
import javax.swing.ProgressMonitorInputStream;
import javax.swing.SwingWorker;

public class FileDownloader {
	Query q = new Query();
	public static final int NORMAL = 0;
	static int percentDone = 0;
	static double count = 0;
	static String filePath;
	static String fileName;
	static int fileSize;
	private Worker w;

	void fileDown() {
		(w = new Worker()).execute();
	}

	private class Worker extends SwingWorker<Object, String> {
		@Override
		protected Void doInBackground() {
			
			percentDone = 0;
			count = 0;
			List<String> myList = new LinkedList<String>();
			try {
				DownloadWindow.jbtDownload.setEnabled(false);
				DownloadWindow.jbtViewLiscence.setEnabled(false);
				java.io.BufferedInputStream in = new java.io.BufferedInputStream(
						new java.net.URL(filePath).openStream());
				
				JLabel filenameLabel = new JLabel(filePath, JLabel.RIGHT);
				filenameLabel.setForeground(Color.black);
				Object message[] = { "Reading:", filenameLabel };

				java.io.FileOutputStream fos = new java.io.FileOutputStream(
						".\\Downloads\\" + fileName);
				java.io.BufferedOutputStream bout = new BufferedOutputStream(
						fos, 1024);
				

				ProgressMonitorInputStream pmis = new ProgressMonitorInputStream(
						null, message, in);
				
					ProgressMonitor pm = pmis.getProgressMonitor();
					
				byte[] data = new byte[1024];
				int x = 0;
				while ((x = pmis.read(data, 0, 1024)) >= 0) {
					bout.write(data, 0, x);
					count += 1024; // seems to be a good number to divide by size

					percentDone = (int) ((count / fileSize) * 100);
					if (FileDownloader.count % (1024) == 0
							&& FileDownloader.percentDone <= 100) {
						myList.add(Query.progName + ": "
								+ FileDownloader.percentDone + "%");
						publish(Query.progName + ": "
								+ FileDownloader.percentDone + "%");
						System.out.println(FileDownloader.percentDone + "%");
					} else if (FileDownloader.count % (1024) == 0
							&& FileDownloader.percentDone > 100) {
						myList.add(Query.progName + ": "
								+ "finishing up download...");
						publish(Query.progName + ": "
								+ "finishing up download...");
						System.out.println("Finishing up download...");
					}
				}

				bout.close();
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			myList.add(Query.progName + ": Finished download!");
			publish(Query.progName + ": Finished download!");

			System.out.println("Finished download! size: " + count);
			DownloadWindow.jbtViewLiscence.setEnabled(true);
			q.userDownloadUpdate();
			return null;
		}

		@Override
		protected void process(List<String> str) {
			updateDownloadLabel(str.get(str.size() - 1));
		}
	}

	public void updateDownloadLabel(String str) {
		DownloadWindow.jlblOutput.setText(str);
	}

}
